---
title: "Jenkins Snippets"
date: 2019-7-24
categories:
- jenkins
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgb078e01">Pipeline</a>
<ul>
<li><a href="#org85d99ed">从SCM中获取上次构建的提交版本</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-orgb078e01">
<h2 id="orgb078e01">Pipeline</h2>
<div class="outline-text-2" id="text-orgb078e01">
</div>
<div class="outline-3" id="outline-container-org85d99ed">
<h3 id="org85d99ed">从SCM中获取上次构建的提交版本</h3>
<div class="outline-text-3" id="text-org85d99ed">
<div class="org-src-container">
<pre class="src src-groovy">pipeline{

    environment{
        APP_NAME = sh(returnStdout: <span style="font-weight: bold; text-decoration: underline;">true</span>, script: <span style="font-style: italic;">'echo $app_name'</span>).trim()
        GIT_USERNAME = sh(returnStdout: <span style="font-weight: bold; text-decoration: underline;">true</span>, script: <span style="font-style: italic;">'echo $gitlabUserName'</span>)

        REF_VAR = sh(returnStdout: <span style="font-weight: bold; text-decoration: underline;">true</span>, script: <span style="font-style: italic;">'echo asdf'</span>).trim()
        PTR_VAR = <span style="font-style: italic;">"</span><span style="font-weight: bold; font-style: italic;">$REF_VAR</span><span style="font-style: italic;">"</span>
    }
    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">定义本次构建使用哪个标签的构建环境，本示例中为 “slave-pipeline”</span>
    agent{
        node{
            label <span style="font-style: italic;">'slave-pipeline'</span>
        }
    }

    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">"stages"定义项目构建的多个模块，可以添加多个 “stage”， 可以多个 “stage” 串行或者并行执行</span>
    stages{
        stage(<span style="font-style: italic;">'prepare env'</span>){
            steps{
                script{

                    <span style="font-weight: bold;">def</span> <span style="font-weight: bold; font-style: italic;">commitHashForBuild</span> = { build -&gt;
                        <span style="font-weight: bold;">def</span> <span style="font-weight: bold; font-style: italic;">scmAction</span> = build?.actions.find { action -&gt; action <span style="font-weight: bold;">instanceof</span> jenkins.scm.api.<span style="font-weight: bold; text-decoration: underline;">SCMRevisionAction</span> }
                        <span style="font-weight: bold;">return</span> scmAction?.revision?.hash
                    }
                    <span style="font-weight: bold;">def</span> <span style="font-weight: bold; font-style: italic;">lastSuccessfulHash</span> = <span style="font-weight: bold; text-decoration: underline;">null</span>
                    <span style="font-weight: bold;">def</span> <span style="font-weight: bold; font-style: italic;">lastSuccessfulBuild</span> = currentBuild.rawBuild.getPreviousSuccessfulBuild()
                    <span style="font-weight: bold;">if</span> ( lastSuccessfulBuild ) {

                        echo lastSuccessfulBuild.<span style="font-weight: bold;">class</span>.toString()
                        <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">org.jenkinsci.plugins.workflow.job.WorkflowRun</span>
                        lastSuccessfulHash = commitHashForBuild( lastSuccessfulBuild )
                    }
                    echo <span style="font-style: italic;">"last commit: </span><span style="font-weight: bold; font-style: italic;">$lastSuccessfulHash</span><span style="font-style: italic;">"</span>
                    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">这样设置的环境变量在其他的stage step中不能使用</span>
                    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">但是下面的step能使用</span>
                    env.LAST_COMMIT=<span style="font-style: italic;">"asdfasdfasdfasdfasdfasdfa"</span>

                }
                echo <span style="font-style: italic;">"</span><span style="font-weight: bold; font-style: italic;">$LAST_COMMIT</span><span style="font-style: italic;">"</span>
                sh <span style="font-style: italic;">'''echo $LAST_COMMIT'''</span>

                sh <span style="font-style: italic;">'''</span>
<span style="font-style: italic;">                echo ======= evn ========</span>
<span style="font-style: italic;">                printenv</span>
<span style="font-style: italic;">                echo ====================</span>
<span style="font-style: italic;">                echo sh $app_name: $app_name</span>
<span style="font-style: italic;">                echo $gitlabUserName</span>
<span style="font-style: italic;">                '''</span>
            }
        }
    }


}

</pre>
</div>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2019-7-24</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
